package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.AddLabelAction;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.shared.LabelBean;

public class CreateLabelHandler
		extends ActionHandler<AddLabelAction, LabelBean>
{

	public CreateLabelHandler(DataStoreAPI service)
	{
		super(service);
	}

	@Override
	public Class<? extends Action<?>> getActionType()
	{
		return AddLabelAction.class;
	}

	@Override
	public LabelBean invoke(AddLabelAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();
		if (!utils.isTeamEditor(action.getTeamId()))
		{
			throw new BackendException(EErrorCodes.NOT_TEAM_EDITOR);
		}

		return dataService.createLabel(action.getTeamId(), action.getName());
	}

}
